<?php
// src/Acme/UserBundle/Entity/UserRepository.php
namespace Acme\UserBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\Mapping\OrderBy;

class UserRepository extends EntityRepository implements UserProviderInterface {
	public function loadUserByUsername($username) {
		$q = $this->createQueryBuilder ( 'u' )->where ( 'u.username = :username OR u.email = :email' )->setParameter ( 'username', $username )->setParameter ( 'email', $username )->getQuery ();
		
		try {
			// The Query::getSingleResult() method throws an exception
			// if there is no record matching the criteria.
			$user = $q->getSingleResult ();
		} catch ( NoResultException $e ) {
			$message = sprintf ( 'Unable to find an active admin AcmeUserBundle:User object identified by "%s".', $username );
			throw new UsernameNotFoundException ( $message, 0, $e );
		}
		
		return $user;
	}
	public function refreshUser(UserInterface $user) {
		$class = get_class ( $user );
		if (! $this->supportsClass ( $class )) {
			throw new UnsupportedUserException ( sprintf ( 'Instances of "%s" are not supported.', $class ) );
		}
		
		return $this->find ( $user->getId () );
	}
	public function supportsClass($class) {
		return $this->getEntityName () === $class || is_subclass_of ( $class, $this->getEntityName () );
	}
	public function findPersonImagesStandartByUserId($id) {
		$query = $this->getEntityManager ()->createQuery ( '
			SELECT i
				FROM AcmeUserBundle:User u
					LEFT JOIN AcmePersonBundle:Person p WITH p.id = u.id 	
					LEFT JOIN AcmePersonBundle:Personimage i WITH i.person = p.id AND i.standart = 1
					WHERE u.id = :id' )->setParameter ( 'id', $id );
		
		try {
			return $query->getResult ();
		} catch ( \Doctrine\ORM\NoResultException $e ) {
			return null;
		}
	}
	public function findPersonImagesByUserId($id) {
		$query = $this->getEntityManager ()->createQuery ( '
			SELECT i
				FROM AcmeUserBundle:User u
					JOIN AcmePersonBundle:Person p WITH p.id = u.id
					JOIN AcmePersonBundle:Personimage i WITH i.person = p.id
					WHERE u.id = :id' )->setParameter ( 'id', $id );
		
		try {
			return $query->getResult ();
		} catch ( \Doctrine\ORM\NoResultException $e ) {
			return null;
		}
	}
	public function findOneByIdJoinedToPersonOrg($id) {
		$query = $this->getEntityManager ()->createQuery ( '
            SELECT p, c FROM AcmePersonBundle:Product p
            JOIN p.category c
            WHERE p.id = :id' )->setParameter ( 'id', $id );
		
		try {
			return $query->getSingleResult ();
		} catch ( \Doctrine\ORM\NoResultException $e ) {
			return null;
		}
	}
	public function getActive() {
		// As may have notice, the delay is redondant, for the sake of the example, i didn't defined its value in my bundle configuration, but it would be better to do so.
		$delay = new \DateTime ();
		$delay->setTimestamp ( strtotime ( '2 minutes ago' ) );
		
		$qb = $this->createQueryBuilder ( 'u' )->where ( 'u.lastActivity > :delay' )->setParameter ( 'delay', $delay );
		
		return $qb->getQuery ()->getResult ();
	}
	public function findAll() {
		return $this->findBy ( array (), array (
				'username' => 'ASC' 
		) );
	}
	public function findAllPerson() {
		$query = $this->getEntityManager ()->createQuery ( '
			SELECT p.firstname, u.id, u.email
				FROM AcmeUserBundle:User u
					JOIN AcmePersonBundle:Person p WITH p.id = u.id
    				ORDER BY p.lastname ASC' );
		
		try {
			return $query->getResult ();
		} catch ( \Doctrine\ORM\NoResultException $e ) {
			return null;
		}
	}
}

?>